package cn.learn.service.cargo.impl;

import cn.learn.dao.cargo.PackingListDao;
import cn.learn.dao.cargo.ShippingOrderDao;
import cn.learn.domain.cargo.PackingList;
import cn.learn.domain.cargo.ShippingOrder;
import cn.learn.domain.cargo.ShippingOrderExample;
import cn.learn.service.cargo.ShippingOrderService;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.List;

@Service
public class ShippingOrderServiceImpl implements ShippingOrderService {

    @Autowired
    private ShippingOrderDao shippingOrderDao;

    @Autowired
    private PackingListDao packingListDao;
    @Override
    public PageInfo findAll(ShippingOrderExample shippingOrderExample, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<ShippingOrder> list = shippingOrderDao.selectByExample(shippingOrderExample);
        return new PageInfo(list);
    }

    @Override
    public void save(ShippingOrder shippingOrder) {
        shippingOrder.setCreateTime(new Date());
        shippingOrder.setState(0);
        PackingList packingList = packingListDao.selectByPrimaryKey(shippingOrder.getShippingOrderId());
        packingList.setState(2L);
        packingListDao.updateByPrimaryKeySelective(packingList);
        shippingOrderDao.insertSelective(shippingOrder);
    }

    @Override
    public ShippingOrder findById(String id) {
        ShippingOrder shippingOrder = shippingOrderDao.selectByPrimaryKey(id);
        return shippingOrder;
    }

    @Override
    public void update(ShippingOrder shippingOrder) {
        shippingOrderDao.updateByPrimaryKeySelective(shippingOrder);
    }

    @Override
    public void delete(String id) {
        shippingOrderDao.deleteByPrimaryKey(id);
    }

}
